package com.wwwarehouse.taskcenter.fragment.invite_business_information;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.OwnBusinessUnitBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/11 9:17
 * @描述 ${TODO}
 */
@Route(path = TaskCenterConstant.OwnBusinessUnitFragment)
public class OwnBusinessUnitFragment extends BaseFragment {

    private View                    mView;
    private StateLayout             mStateLayout;
    private SlideBarView            mSlideBarView;
    private CustomViewPagerInternal mVpContain;
    private CirclePageIndicator     mPageIndicate;
    private Map                     mOwnBusinessMap;
    private List<Fragment>          mFragmentList;
    private Bundle mOwnBusiUnitToChildBundle;
    private OwnBusinessUnitFragmentChild mOwnBusinessUnitFragmentChild;
    private ArrayList<OwnBusinessUnitBean> mOwnBusinessUnitList;
    private int mInviteeId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_own_business_unit, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mStateLayout = findView(mView, R.id.sl);
        mVpContain = findView(mView, R.id.cvpi);
        mPageIndicate = findView(mView, R.id.cpi);
        mSlideBarView = findView(mView, R.id.sbv_slide);

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof OwnBusinessUnitFragment) {
            mActivity.setTitle(R.string.own_buss_unit);
        }
    }

    @Override
    public void requestDatas() {
        mFragmentList = new ArrayList<>();
        mOwnBusinessMap = new HashMap();
        Bundle ownBusinessUnitBundle = getArguments();
        if (ownBusinessUnitBundle != null) {
            mInviteeId = ownBusinessUnitBundle.getInt("inviteeId");
            mOwnBusinessMap.put("inviteeId", mInviteeId);
        }
        getOwnBusinessUnit();
    }

    private void getOwnBusinessUnit(){

        mStateLayout.showProgressView(true);//加载时候的状态
        NoHttpUtils.httpPost(TaskCenterConstant.BUSINESS_UNITS_BY_INVITER, mOwnBusinessMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener=new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                switch (i) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {
                            mOwnBusinessUnitList = (ArrayList<OwnBusinessUnitBean>) JSON.parseArray(commonClass.getData().toString(), OwnBusinessUnitBean.class);
                            int total = mOwnBusinessUnitList.size();
                            if (total == 0) {
                                mStateLayout.showEmptyView(true);//请求回来为空
                                mStateLayout.setEmptyListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        getOwnBusinessUnit();
                                    }
                                });
                            } else {
                                final int page = (total % 7 == 0) ? total / 7 : (total / 7 + 1);
                                mFragmentList.clear();
                                for (int j = 0; j < page; j++) {
                                    mOwnBusiUnitToChildBundle = new Bundle();
                                    newFragment(j);
                                    mOwnBusinessUnitFragmentChild.setArguments(mOwnBusiUnitToChildBundle);
                                    mFragmentList.add(mOwnBusinessUnitFragmentChild);
                                }

                                mVpContain.setAdapter(new ContractFragmentPagerAdapter(getChildFragmentManager(), mFragmentList));
                                if (page <= 6) {
                                    mPageIndicate.setVisibility(View.VISIBLE);
                                    mSlideBarView.setVisibility(View.GONE);
                                    mPageIndicate.setViewPager(mVpContain, 0);
                                } else {
                                    mPageIndicate.setVisibility(View.GONE);
                                    mSlideBarView.setVisibility(View.VISIBLE);
                                    mSlideBarView.setTotalPage(page);
                                    mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                                        @Override
                                        public void onSlideChange(int i) {
                                            mVpContain.setCurrentItem(page, false);
                                        }
                                    });
                                    mSlideBarView.setViewPagerInternal(mVpContain);
                                }

                            }
                        } else {
                            mStateLayout.showSystemView(commonClass.getMsg(), true);
                            mStateLayout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    getOwnBusinessUnit();
                                }
                            });
                        }
                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);//网络问题
            mStateLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getOwnBusinessUnit();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    private void newFragment(int j) {
        mOwnBusinessUnitFragmentChild = new OwnBusinessUnitFragmentChild();
        mOwnBusiUnitToChildBundle.putInt("page", j + 1);
        mOwnBusiUnitToChildBundle.putInt("inviteeId", mInviteeId);
    }
}
